//package com.asiainfo.iov.fragment;
//
//import android.content.Context;
//import android.os.Bundle;
//import android.os.Handler;
//import android.os.Message;
//import android.support.annotation.NonNull;
//import android.support.v4.app.Fragment;
//import android.support.v7.widget.LinearLayoutManager;
//import android.text.TextUtils;
//import android.view.LayoutInflater;
//import android.view.View;
//import android.view.ViewGroup;
//import android.view.WindowManager;
//import android.widget.LinearLayout;
//import android.widget.RelativeLayout;
//
//import com.framework.application.FrameApplication;
//import com.framework.customviews.SwipeMenuView;
//import com.framework.utils.CollectionUtil;
//import com.framework.utils.ScreenUtils;
//import com.framework.utils.ToastUtil;
//import com.framework.utils.Y;
//import com.framework2.baseEvent.BaseOnClickListener;
//import com.framework2.customLoading.LoadingFooter;
//import com.framework2.customLoading.indicators.BallSpinFadeLoaderIndicator;
//import com.framework2.greendao.GreenDaoManager;
//import com.framework2.utils.CustomLoadingDialogUtils;
//import com.library.adapter_recyclerview.UniversalAdapter;
//import com.library.pulltorefresh.BaseAbstractPullToRefreshLayout;
//import com.library.pulltorefresh.classical.SoftClassicalPullToRefreshLayout;
//import com.library.pulltorefresh.pullableview.PullableRecyclerView;
//import com.yaxin.yyt.R;
//import com.yaxin.yyt.activity.BaseActivity;
//import com.yaxin.yyt.activity.ShopMessageDetailActivity;
//import com.yaxin.yyt.configs.ConstantsME;
//import com.yaxin.yyt.configs.InterfaceConfig;
//import com.yaxin.yyt.entity.MessageInfo;
//import com.yaxin.yyt.networkModel.HttpUtil;
//
//import java.text.SimpleDateFormat;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//import java.util.Locale;
//
///**
// * 消息中心--店铺消息
// *
// * @author YobertJomi
// * className Fragment_shopMessage
// * created at  2017/3/16  15:39
// */
//public class Fragment_shopMessage extends Fragment implements PullableRecyclerView.OnAutoLoadListener {
//    private Context mContext;
//    private View view;// infalte的布局
//    private LinearLayout containerLayout;// 新建容器
//    ////////////////////////////////
//    private SoftClassicalPullToRefreshLayout refreshLayout;
//    private PullableRecyclerView recyclerView;
//    private UniversalAdapter adapter;
//    private List<MessageInfo> infoList = new ArrayList<>();
//    private LoadingFooter footerView;
//    private boolean requestSuccess;
//    private int page = 1;
//    private int pageSize = 30;
//    ////////////////////////////////
//    private SimpleDateFormat simpleDateFormat;
//
//    private MessageInfo newMessageInfo;
//    private BaseOnClickListener onDeleteClickListener = new BaseOnClickListener() {
//        @Override
//        public void onBaseClick(View v) {
//            MessageInfo info = (MessageInfo) v.getTag(R.id.shopMessageDeleteTag);
//            if (null != info) {
//                if (null != v.getTag(R.id.shopMessageDeletePositionTag)) {
//                    int position = (int) v.getTag(R.id.shopMessageDeletePositionTag);
////                    List list = GreenDaoManager.getInstance().queryEntityList(mContext, MessageInfo.class);
////                    if (!CollectionUtil.getInstance().isEmpty(list)) {
////                        for (int i = 0; i < list.size(); i++) {
////                            MessageInfo infoTemp = (MessageInfo) list.get(i);
////                            if (TextUtils.equals(info.getId(), infoTemp.getId()) && !infoTemp.getIsDeleted()) {
////                                infoTemp.setIsDeleted(true);
////                                GreenDaoManager.getInstance().updateEntity(mContext, infoTemp);
////                            }
////                        }
////                    }
////                    adapter.deleteData(position);
//                    CustomLoadingDialogUtils.showProcessDialog(mContext, "删除消息");
//                    requestDeleteOneMessage(info.getId(), position);
//                }
//            }
//        }
//    };
//    private BaseOnClickListener onReadedClickListener = new BaseOnClickListener() {
//        @Override
//        public void onBaseClick(View v) {
//            final MessageInfo info = (MessageInfo) v.getTag(R.id.shopMessageReadedTag);
//            if (null != info) {
//                if (null != v.getTag(R.id.shopMessageReadedPositionTag)) {
//                    final int position = (int) v.getTag(R.id.shopMessageReadedPositionTag);
////                    List list = GreenDaoManager.getInstance().queryEntityList(mContext, MessageInfo.class);
////                    if (!CollectionUtil.getInstance().isEmpty(list)) {
////                        for (int i = 0; i < list.size(); i++) {
////                            MessageInfo infoTemp = (MessageInfo) list.get(i);
////                            if (TextUtils.equals(info.getId(), infoTemp.getId()) && !infoTemp.getIsReaded()) {
////                                infoTemp.setIsReaded(true);
////                                GreenDaoManager.getInstance().updateEntity(mContext, infoTemp);
////                            }
////                        }
////                    }
////                    if (null != v.getTag(R.id.shopMessageReadedViewHolderTag)) {
////                        UniversalAdapter.UniversalViewHolder viewHolder = (UniversalAdapter.UniversalViewHolder) v
//// .getTag(R.id.shopMessageReadedViewHolderTag);
////                        if (null != viewHolder) {
////                            ((SwipeMenuView) viewHolder.itemView).smoothClose();
////                            Utils.h.postDelayed(new Runnable() {
////                                @Override
////                                public void run() {
////                                    info.setIsReaded(true);
////                                    adapter.notifyItemChanged(position);
////                                }
////                            }, 300);
////                        }
////                    }
//                    CustomLoadingDialogUtils.showProcessDialog(mContext, "标记已读");
//                    if (null != v.getTag(R.id.shopMessageReadedViewHolderTag)) {
//                        UniversalAdapter.UniversalViewHolder viewHolder = (UniversalAdapter.UniversalViewHolder) v
//                                .getTag(R.id.shopMessageReadedViewHolderTag);
//                        if (null != viewHolder) {
//                            requestRemarkOneMessage(info, viewHolder, position);
//                        }
//                    }
//                }
//            }
//        }
//    };
//    private BaseOnClickListener onItemClickListener = new BaseOnClickListener() {
//        @Override
//        public void onBaseClick(View v) {
//            final MessageInfo info = (MessageInfo) v.getTag(R.id.shopMessageTag);
//            if (null != info) {
//                if (null != v.getTag(R.id.shopMessageReadedPositionTag)) {
//                    final int position = (int) v.getTag(R.id.shopMessageReadedPositionTag);
//                    List list = GreenDaoManager.getInstance().queryEntityList(mContext, MessageInfo.class);
//                    if (!CollectionUtil.getInstance().isEmpty(list)) {
//                        for (int i = 0; i < list.size(); i++) {
//                            MessageInfo infoTemp = (MessageInfo) list.get(i);
//                            Y.y("内容:" + infoTemp.getContent() + "   " + infoTemp.getId() + "    " + infoTemp
//                                    .getIsReaded());
//                            if (TextUtils.equals(info.getId(), infoTemp.getId()) && !infoTemp.getIsReaded()) {
//                                infoTemp.setIsReaded(true);
//                                GreenDaoManager.getInstance().updateEntity(mContext, infoTemp);
//                            }
//                        }
//                    }
//                    if (!info.getIsReaded())
//                        requestRemarkOneMessage(info, null, position);
//                    info.setIsReaded(true);
//                    adapter.notifyItemChanged(position);
//                    ((BaseActivity) getContext()).startActivity(ShopMessageDetailActivity.class, info);
//                }
//            }
//        }
//    };
//
//    @Override
//    public void onCreate(Bundle savedInstanceState) {
//        super.onCreate(savedInstanceState);
//        mContext = getActivity();
//        Bundle arguments = getArguments();
//        if (null != arguments && null != arguments.getSerializable(ConstantsME.entity)) {
//            newMessageInfo = (MessageInfo) arguments.getSerializable(ConstantsME.entity);
//        }
//    }
//
//    public View onCreateView(LayoutInflater inflater,
//                             ViewGroup container, Bundle savedInstanceState) {
//        if (null == view) {
//            containerLayout = new LinearLayout(mContext);
//            view = inflater.inflate(R.layout.fragment_message, null);
//            view.setMinimumHeight(((WindowManager) getActivity()
//                    .getSystemService(Context.WINDOW_SERVICE))
//                    .getDefaultDisplay().getHeight());
//            view.setMinimumWidth(((WindowManager) getActivity()
//                    .getSystemService(Context.WINDOW_SERVICE))
//                    .getDefaultDisplay().getWidth());
//            containerLayout.addView(view);
//        } else {
//            containerLayout.removeAllViews();
//            containerLayout = new LinearLayout(getActivity());
//            containerLayout.addView(view);
//        }
//        initView();
//        initRecyclerView();
//        if (!requestSuccess) {
//            CustomLoadingDialogUtils.showProcessDialog(mContext, "店铺消息");
//            requestShopMessageList();
//        }
//        return containerLayout;
//    }
//
//    private void initView() {
//        recyclerView = (PullableRecyclerView) view.findViewById(R.id.recyclerView);
//        refreshLayout = (SoftClassicalPullToRefreshLayout) view.findViewById(R.id.refresh_view);
//    }
//
//    private View initFooter() {
//        footerView = new LoadingFooter(mContext);
//        footerView.setLoadingHint("加载更多");
//        footerView.setNoMoreHint("到底了");
//        footerView.setNoNetWorkHint("网络不给力啊，点击再试一次吧");
//        footerView.setState(LoadingFooter.State.Loading);
//        footerView.setProgressStyle(BallSpinFadeLoaderIndicator.class);
//        return footerView;
//    }
//
//    @SuppressWarnings("unchecked")
//    private void initRecyclerView() {
//        recyclerView.setHasFixedSize(true);
//        recyclerView.setCanPullDown(true);
//        recyclerView.setCanPullUp(false);
//        recyclerView.setHasMoreData(false);
//        recyclerView.setOnAutoLoadListener(this);
//        refreshLayout.setRefreshContentTextView("店铺消息");
//        refreshLayout.setLoadContentTextView("店铺消息");
//        refreshLayout.setEmptyTxt("店铺消息为空,点击刷新");
//        refreshLayout.setOnRefreshListener(new BaseAbstractPullToRefreshLayout.OnRefreshListener() {
//            @Override
//            public void onRefresh(final BaseAbstractPullToRefreshLayout pullToRefreshLayout) {
//                // 下拉刷新操作
//                new Handler() {
//                    @Override
//                    public void handleMessage(Message msg) {
//                        // 千万别忘了告诉控件刷新完毕了哦！
//                        // pullToRefreshLayout.refreshFinish(BaseAbstractPullToRefreshLayout.SUCCEED);
//                        CustomLoadingDialogUtils.showProcessDialog(mContext, "店铺消息");
//                        requestShopMessageList();
//                    }
//                }.sendEmptyMessageDelayed(0, 100);
//            }
//
//            @Override
//            public void onLoadMore(final BaseAbstractPullToRefreshLayout pullToRefreshLayout) {
//                // 加载操作
//                new Handler() {
//                    @Override
//                    public void handleMessage(Message msg) {
//                        // 千万别忘了告诉控件加载完毕了哦！
//                        // pullToRefreshLayout.loadmoreFinish(BaseAbstractPullToRefreshLayout.SUCCEED);
//                    }
//                }.sendEmptyMessageDelayed(0, 100);
//            }
//        });
//        //第一种，简单的颜色，高度,水平分割线
//        recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
//        initDB();
//        if (CollectionUtil.getInstance().isEmpty(infoList)) {
//            refreshLayout.showEmptyView();
//        }
//        adapter = initAdapter(adapter);
//        recyclerView.setAdapter(adapter);
//    }
//
//    /**
//     * @param adapter adapter
//     * @return UniversalAdapter
//     */
//    @SuppressWarnings("unchecked")
//    private UniversalAdapter initAdapter(UniversalAdapter adapter) {
//        if (adapter == null) {
//            adapter = new UniversalAdapter<MessageInfo>(getActivity(), R.layout.listitem_shopmessage, infoList) {
//                @Override
//                protected void getItemView(UniversalViewHolder viewHolder, MessageInfo info, int position) {
//                    if (null == simpleDateFormat) {
//                        simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
//                    }
//                    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams
//                            .MATCH_PARENT, (int) ((ScreenUtils.getInstance().getScreenHeightPx(mContext) -
//                            ScreenUtils.getInstance().getStatusBarHeightPx(mContext) - ScreenUtils.getInstance()
//                            .dip2px(mContext, 50)) * 0.11));
//                    viewHolder.itemView.setLayoutParams(params);
//                    //这句话关掉IOS阻塞式交互效果 并依次打开左滑右滑
//                    ((SwipeMenuView) viewHolder.itemView).setIos(true).setLeftSwipe(true);
//                    viewHolder.setImageResource(R.id.icon_remark, info.getIsReaded() ? R.drawable
//                            .shape_circle_gray_10dp : R.drawable.shape_circle_blue);
//                    viewHolder.setText(R.id.tv_title, info.getTitle());
//                    viewHolder.setText(R.id.tv_content, info.getContent());
//                    try {
//                        String date = simpleDateFormat.format(new Date(Long.parseLong(info.getDate()) * 1000));
//                        info.setDate(date);
//                    } catch (NumberFormatException e) {
//                        e.printStackTrace();
//                    }
//                    viewHolder.setText(R.id.tv_time, info.getDate());
//
//                    viewHolder.getView(R.id.btnReaded).setTag(R.id.shopMessageReadedTag, info);//已读
//                    viewHolder.getView(R.id.btnReaded).setTag(R.id.shopMessageReadedViewHolderTag, viewHolder);
//                    viewHolder.getView(R.id.btnReaded).setTag(R.id.shopMessageReadedPositionTag, position);
//                    viewHolder.getView(R.id.btnReaded).setOnClickListener(onReadedClickListener);
//
//                    viewHolder.getView(R.id.btnDelete).setTag(R.id.shopMessageDeleteTag, info);//删除
//                    viewHolder.getView(R.id.btnDelete).setTag(R.id.shopMessageDeletePositionTag, position);
//                    viewHolder.getView(R.id.btnDelete).setOnClickListener(onDeleteClickListener);
//
//                    viewHolder.getView(R.id.rootView).setTag(R.id.shopMessageReadedViewHolderTag, viewHolder);
//                    viewHolder.getView(R.id.rootView).setTag(R.id.shopMessageReadedPositionTag, position);
//                    viewHolder.getView(R.id.rootView).setTag(R.id.shopMessageTag, info);//item点击 //已读
//                    viewHolder.getView(R.id.rootView).setOnClickListener(onItemClickListener);
//                }
//            };
//        }
//        return adapter;
//    }
//
//    /**
//     * 标记已读/删除
//     */
//    @SuppressWarnings("unchecked")
//    private void initDB() {
//        if (!CollectionUtil.getInstance().isEmpty(infoList)) {
//            List<MessageInfo> list = GreenDaoManager.getInstance().queryEntityList(mContext, MessageInfo.class);
//            if (!CollectionUtil.getInstance().isEmpty(list)) {
//                int size = list.size();
//                int sizeInfo = infoList.size();
//                List<Integer> removeList = null;
//                for (int j = sizeInfo - 1; j >= 0; j--) {
//                    for (int i = 0; i < size; i++) {
//                        if (TextUtils.equals(list.get(i).getId(), infoList.get(j).getId()) && list.get(i)
//                                .getIsDeleted()) {
//                            infoList.get(j).setIsDeleted(true);
//                            if (null == removeList)
//                                removeList = new ArrayList<>();
//                            removeList.add(j);
//                            break;
//                        }
//                        if (TextUtils.equals(list.get(i).getId(), infoList.get(j).getId()) && list.get(i).getIsReaded
//                                ()) {
//                            infoList.get(j).setIsReaded(true);
//                            break;
//                        }
//                    }
//                }
//                GreenDaoManager.getInstance().deleteAll(mContext, MessageInfo.class);
//                boolean b = GreenDaoManager.getInstance().insertEntityList(mContext, MessageInfo.class, infoList);
//                if (!CollectionUtil.getInstance().isEmpty(removeList)) {
//                    for (int i = 0; i < removeList.size(); i++) {
//                        infoList.remove((int) (removeList.get(i)));
//                    }
//                }
//            } else {
//                boolean a = GreenDaoManager.getInstance().insertEntityList(mContext, MessageInfo.class, infoList);
//            }
//        } else {
//            infoList = GreenDaoManager.getInstance().queryEntityList(mContext, MessageInfo.class);
//        }
//    }
//
//    @SuppressWarnings("unchecked")
//    private void saveLastMessageIntoDB(List<MessageInfo> list) {
//        if (page == 1 && CollectionUtil.getInstance().isEmpty(list)) {
//            GreenDaoManager.getInstance().deleteAll(mContext, MessageInfo.class);
//            GreenDaoManager.getInstance().insertEntityList(mContext, MessageInfo.class, list);
//        }
//    }
//
//    /**
//     * 如果是从首页跳转，将执行这个方法，会标记最新消息已读
//     *
//     * @param list
//     */
//    private void dealNewMessage(@NonNull List<MessageInfo> list) {
//        if (newMessageInfo != null) {
//            if (TextUtils.equals(newMessageInfo.getId(), list.get(0).getId())) {
//                requestRemarkOneMessage(list.get(0), null, 0);
//            }
//        }
//    }
//
//    private void requestShopMessageList() {
//        HttpUtil.getInstance().requestShopMessageList((BaseActivity) mContext, InterfaceConfig.shopMessageList, page,
//                pageSize, new HttpUtil.OnRequestListResult<MessageInfo>() {
//            @Override
//            public void onSuccess(List<MessageInfo> list, String... msg) {
//                saveLastMessageIntoDB(list);
//                requestSuccess = true;
//                refreshLayout.refreshFinish(BaseAbstractPullToRefreshLayout.SUCCEED);
//                recyclerView.setOnLoading(false);
//                CustomLoadingDialogUtils.dismissProcessDialog();
//                if (!CollectionUtil.getInstance().isEmpty(list)) {
//                    if (page == 1) {
//                        dealNewMessage(list);
//                        infoList.clear();
//                    }
//                    infoList.addAll(list);
//                    adapter.notifyDataSetChanged();
//                } else if (CollectionUtil.getInstance().isEmpty(infoList)) {
//                    page = 1;
//                    refreshLayout.showEmptyView();
//                }
//                try {
//                    String totalCount = msg[0];
//                    int totalCount_ = Integer.parseInt(totalCount);
//                    recyclerView.setHasMoreData(page * pageSize < totalCount_);
//                    if (page * pageSize >= totalCount_) {
//                        if (footerView != null) {
//                            footerView.setState(LoadingFooter.State.NoMore);
//                        }
//                    } else {
//                        if (adapter.getFooterCount() <= 0) {
//                            adapter.addFooterViewAfterSetAdapter(initFooter());
//                        }
//                        page++;
//                    }
//                } catch (Exception e) {
//                    recyclerView.setHasMoreData(false);
//                    if (footerView != null) {
//                        footerView.setState(LoadingFooter.State.NoMore);
//                    }
//                }
//            }
//
//            @Override
//            public void onFail(int code, String msg) {
//                refreshLayout.refreshFinish(BaseAbstractPullToRefreshLayout.FAIL);
//                recyclerView.setOnLoading(false);
//                CustomLoadingDialogUtils.dismissProcessDialog();
//                ToastUtil.getInstance().showToast(TextUtils.isEmpty(msg) ? "获取店铺消息失败" : msg);
//            }
//        });
//    }
//
//    /**
//     * 删除消息
//     *
//     * @param msgId    msgId
//     * @param position position
//     */
//    private void requestDeleteOneMessage(String msgId, final int position) {
//        HttpUtil.getInstance().requestDeleteOneMessage((BaseActivity) mContext, InterfaceConfig.deleteOneShopMsg,
//                msgId, new HttpUtil.OnRequestResult<String>() {
//            @Override
//            public void onSuccess(String... msg) {
//                CustomLoadingDialogUtils.dismissProcessDialog();
//                adapter.deleteData(position);
//                if (null != msg) {
//                    ToastUtil.getInstance().showToast("删除消息成功");
//                }
//            }
//
//            @Override
//            public void onFail(int code, String msg) {
//                CustomLoadingDialogUtils.dismissProcessDialog();
//                ToastUtil.getInstance().showToast(TextUtils.isEmpty(msg) ? "删除消息失败" : msg);
//            }
//        });
//    }
//
//    /**
//     * 店铺消息----标记已读
//     *
//     * @param info     MessageInfo对象
//     * @param position position
//     */
//    private void requestRemarkOneMessage(final MessageInfo info, final UniversalAdapter.UniversalViewHolder
//            viewHolder, final int position) {
//        HttpUtil.getInstance().requestRemarkOneMessage((BaseActivity) mContext, InterfaceConfig.remarkOneShopMsg,
//                info.getId(), new HttpUtil.OnRequestResult<String>() {
//            @Override
//            public void onSuccess(String... msg) {
//                CustomLoadingDialogUtils.dismissProcessDialog();
//                if (null != viewHolder) {
//                    ((SwipeMenuView) viewHolder.itemView).smoothClose();
//                    FrameApplication.mHandler.postDelayed(new Runnable() {
//                        @Override
//                        public void run() {
//                            info.setIsReaded(true);
//                            adapter.notifyItemChanged(position);
//                        }
//                    }, 300);
//                    if (null != msg) {
//                        ToastUtil.getInstance().showToast("标记已读成功");
//                    }
//                }
//            }
//
//            @Override
//            public void onFail(int code, String msg) {
//                CustomLoadingDialogUtils.dismissProcessDialog();
//                if (viewHolder != null) {
//                    ToastUtil.getInstance().showToast(TextUtils.isEmpty(msg) ? "标记已读失败" : msg);
//                }
//            }
//        });
//    }
//
//    @Override
//    public void onAutoLoad(final PullableRecyclerView pullableRecyclerView) {
//        pullableRecyclerView.setOnLoading(true);
//        new Handler() {
//            @Override
//            public void handleMessage(Message msg) {
//                requestShopMessageList();
//            }
//        }.sendEmptyMessageDelayed(0, 100);
//    }
//}
